from random import randint
from time import time
from Tkinter import PhotoImage
from math import sqrt
from weapons import *
from Zombie import Zombie
from pygame import mixer

class Player:
	#array of the current 'shots' fired
	shot = []
	
	#time of last attack
	last_attack_time = time()
	last_shot = time()-10
	
	last_time = time()-10
	
	#time variable used for time-based score incrementation
	score_time = time()
	
	#time variable - last time that the warning sound was played
	last_played = time()-last_time
	
	#player movement speed
	speed = 2
	
	color = "#00F"
	zombified = False
	health_stage = "good"
	
	health = 100
	old_health = 100
	
	hurt_image = 0
	hurting = False
	
	direction = 'down'
	
	weapon = 0
	weapons = ['fire','chainsaw','shotgun','pistol']
	
	current_menu_item = 0
	last_menu_item = 0
	menu_items = ['resume','reset city']
	menu_text = []
	
	score = 0
	paused = False
	
	def __init__(self, root, canvas, hud, buildings, zombies, humans):
		self.buildings = buildings
		self.hud = hud
		self.canvas = canvas
		
		self.photo_hurt = PhotoImage(file="hurt.gif")
	
		self.zombies = zombies
		self.humans = humans
		
		self.hud_back = PhotoImage(file="H.U.D.gif")
		self.hud.create_image((300,35),image=self.hud_back)
		
		self.photo_head = PhotoImage(file="bruce_normal.gif")
		self.head_image = self.hud.create_image((300,35),image=self.photo_head)
		
		self.photo_health = PhotoImage(file="health100.gif")
		self.health_image = self.hud.create_image((210,35),image=self.photo_health)
		
		self.weapon_images = [PhotoImage(file="WepBarFLAME.gif"),PhotoImage(file="WepBarCHAIN.gif"),PhotoImage(file="WepBarSHOT.gif"),PhotoImage(file="WepBarPISTOL.gif")]
		self.weapon_image = self.hud.create_image((470,35),image=self.weapon_images[0])
		
		self.score_rect = self.hud.create_rectangle(0,0,160,70,fill="#000")
		self.score_head = self.hud.create_text((40,15),text="Score:",fill="#a11",font=("Helvectica", "16"))
		self.score_text = self.hud.create_text((60,45),text=self.score,fill="#f00",font=("Helvectica", "24"))
		
		#assume a collision for our first loop
		collision = True
		while(collision):
			self.x = randint(0,600)
			self.y = randint(0,400)
			collision = False
			for building in self.buildings:
				if building.hit_test(self.x,self.y):
					collision = True
		self.root = root
		self.canvas = canvas
		self.rectangle = self.canvas.create_rectangle(self.x,self.y,self.x,self.y,fill=self.color,outline=self.color)
		self.circle = self.canvas.create_oval(self.x-5,self.y-5,self.x+5,self.y+5,fill='',outline=self.color)
	def get_x(self):
		return self.x
	def get_y(self):
		return self.y
	def update(self):
		if self.health_stage != "dead":
			if time()-self.score_time>2:
				self.score += 10
				self.score_time = time()
			for shot in self.shot:
				shot.update()
			if self.zombified and time()-self.last_time>=30:
				self.health_stage = 'dead'
			elif self.zombified:
				if hasattr(self,"time_left_text"):
					self.canvas.delete(self.time_left_text)
				time_left = "%d seconds until starvation" % (30-(time()-self.last_time))
				if (time()-self.last_time)>25 and int(time()-self.last_time)!=self.last_played:
					sound = mixer.Sound("warning.ogg")
					sound.play()
					self.played_sound = True
					self.time_left_text = self.canvas.create_text(100,10,text=time_left,fill="#afa",font=("Helvectica", "12"))
					
				else:
					self.time_left_text = self.canvas.create_text(100,10,text=time_left,fill="#0f0",font=("Helvectica", "12"))
				self.last_played = int(time()-self.last_time)
				
			if self.health <= 75 and self.health > 50 and self.health_stage == "good":
				mixer.Sound("crunch.ogg").play()
				self.photo_head = PhotoImage(file="Bruce75Percent.gif")
				self.hud.delete(self.head_image)
				self.head_image = self.hud.create_image((300,35),image=self.photo_head)
				self.photo_health = PhotoImage(file="health75.gif")
				self.hud.delete(self.health_image)
				self.health_stage = "fine"
				self.health_image = self.hud.create_image((210,35),image=self.photo_health)
			elif self.health <= 50 and self.health > 25 and self.health_stage == "fine":
				mixer.Sound("crunch.ogg").play()
				self.photo_head = PhotoImage(file="Bruce50Percent.gif")
				self.hud.delete(self.head_image)
				self.head_image = self.hud.create_image((300,35),image=self.photo_head)
				self.photo_health = PhotoImage(file="health50.gif")
				self.hud.delete(self.health_image)
				self.health_stage = "hurt"
				self.health_image = self.hud.create_image((210,35),image=self.photo_health)
			elif self.health <= 25 and self.health > 0 and self.health_stage == "hurt":
				mixer.Sound("crunch.ogg").play()
				self.photo_head = PhotoImage(file="bruce_neardeath.gif")
				self.hud.delete(self.head_image)
				self.head_image = self.hud.create_image((300,35),image=self.photo_head)
				self.photo_health = PhotoImage(file="health25.gif")
				self.hud.delete(self.health_image)
				self.health_stage = "near_death"
				self.health_image = self.hud.create_image((210,35),image=self.photo_health)
			elif self.health <= 0 and self.health_stage == "near_death":
				mixer.Sound("crunch.ogg").play()
				self.health_stage = "zombified"
				self.photo_head = PhotoImage(file="bruce_zombie.gif")
				self.hud.delete(self.head_image)
				self.head_image = self.hud.create_image((300,35),image=self.photo_head)
				self.photo_health = PhotoImage(file="health0.gif")
				self.hud.delete(self.health_image)
				self.health_image = self.hud.create_image((210,35),image=self.photo_health)
				self.color = "#0F0"
				self.canvas.delete(self.rectangle)
				self.canvas.delete(self.circle)
				self.rectangle = self.canvas.create_rectangle(self.x,self.y,self.x,self.y,fill=self.color,outline=self.color)
				self.circle = self.canvas.create_oval(self.x-5,self.y-5,self.x+5,self.y+5,fill='',outline=self.color)
				self.zombified = True
				self.last_time = time()
				self.speed = 2
			if not self.hurting and self.health < self.old_health:
				thistime = time()
				if thistime > self.last_attack_time+1:
					mixer.Sound("zombie attack.wav").play()
					self.last_attack_time = thistime
				self.hurt_image = self.hud.create_image((300,35),image=self.photo_hurt)
				self.hurting = True
			else:
				self.hud.delete(self.hurt_image)
				self.hurting = False
			self.old_health = self.health
			self.hud.delete(self.score_text)
			self.score_text = self.hud.create_text((60,45),text=self.score,fill="#f00",font=("Helvectica", "24"))
	def move(self, event):
		if self.paused:
			if event.keysym == 'Up' or event.char == 'w':
				self.last_menu_item = self.current_menu_item
				if self.current_menu_item == 0:
					self.current_menu_item = len(self.menu_items)-1
				else:
					self.current_menu_item -= 1
			elif event.keysym == 'Down' or event.char == 's':
				self.last_menu_item = self.current_menu_item
				if self.current_menu_item == len(self.menu_items)-1:
					self.current_menu_item = 0
				else:
					self.current_menu_item += 1
			elif event.keysym == 'space' or event.keysym == 'Return':
					self.paused = False
		else:
			if self.x >= 600 or self.x <= 0 or self.y >= 400 or self.y <= 0:
					oldx = self.x
					oldy = self.y
					if self.x >= 597:
						self.x = 596
					elif self.x <= 3:
						self.x = 4
					if self.y >= 397:
						self.y = 396
					elif self.y <= 3:
						self.y = 4
					self.canvas.move(self.rectangle, self.x-oldx, self.y-oldy)
					self.canvas.move(self.circle, self.x-oldx, self.y-oldy)
			if  event.keysym == 'Left' or event.char == 'a':
				move_x = -4
				move_y = 0
				self.direction = 'left'
			elif event.keysym == 'Right' or event.char == 'd':
				move_x = 4
				move_y = 0
				self.direction = 'right'
			elif event.keysym == 'Up' or event.char == 'w':
				move_x = 0
				move_y = -4
				self.direction = 'up'
			elif event.keysym == 'Down' or event.char == 's':
				move_x = 0
				move_y = 4
				self.direction = 'down'
			else:
				if event.keysym == 'space' and not self.zombified:
					self.fire()
				using_chainsaw = False
				for shot in self.shot:
					if shot.type == 'chainsaw':
						using_chainsaw = True
						break
				if not using_chainsaw:
					if event.char == '[':
						sound = mixer.Sound("click.ogg")
						sound.play()
						if self.weapon == 0:
							self.weapon = len(self.weapons)-1
							self.weapon_image = self.hud.create_image((470,35),image=self.weapon_images[self.weapon])
						else:
							self.weapon -= 1
							self.weapon_image = self.hud.create_image((470,35),image=self.weapon_images[self.weapon])
					if event.char == ']':
						sound = mixer.Sound("click.ogg")
						sound.play()
						if self.weapon == len(self.weapons)-1:
							self.weapon = 0
							self.weapon_image = self.hud.create_image((470,35),image=self.weapon_images[self.weapon])
						else:
							self.weapon += 1
							self.weapon_image = self.hud.create_image((470,35),image=self.weapon_images[self.weapon])
				if event.keysym == "Escape":
					self.current_menu_item = 0
					self.paused = True
				move_x = 0
				move_y = 0
			#assume a collision for our first loop
			collision = True
			while(collision):
				#Determine how far the human will move...
				collision = False
				for building in self.buildings:
					if building.hit_test(self.x+move_x,self.y+move_y):
						collision = True
						if move_x>0:
							move_x -= 1
						elif move_x<0:
							move_x += 1
						elif move_y>0:
							move_y -= 1
						elif move_y<0:
							move_y += 1
						break
				if(not collision):
					break
			#print self.rectangle
			self.x = self.x + move_x
			self.y = self.y + move_y
			if self.zombified:
				for human in self.humans:
					if not human.is_zombie():
						this_distance = sqrt(((self.x-human.get_x())**2)+((self.y-human.get_y())**2))
						if this_distance < 4:
							self.last_time = time()
							if hasattr(self,"time_left_text"):
								self.canvas.delete(self.time_left_text)
							time_left = "30 seconds until starvation"
							self.time_left_text = self.canvas.create_text(120,10,text=time_left,fill="#fff",font=("Helvectica", "14"))
							new_zom = Zombie(self.root,self.canvas,self.buildings)
							#human.get_thread().add_being(new_zom)
							#human.get_thread().remove_being(human)
							new_zom.set_position(human.get_x(),human.get_y())
							new_zom.know_beings(self.humans,self.zombies,self)
							self.zombies.append(new_zom)
							human.canvas.delete(human.rectangle)
							human.zombified = True
							self.humans.remove(human)
							self.score += 50
							mixer.Sound("crunch.ogg").play()
							del human
							break
							#human.color = "#0f0"
							#human.rectangle = human.canvas.create_rectangle(human.x,human.y,human.x,human.y,fill=human.color,outline='')
						elif this_distance < human.sight_dist:
							if human.chased_by!=0:
								that_distance = sqrt(((human.chased_by.x-human.get_x())**2)+((human.chased_by.y-human.get_y())**2))
							else:
								that_distance = 1000
							if this_distance<that_distance:
								human.panicked = True
								human.color = "red"
								human.canvas.delete(human.rectangle)
								human.speed = 3
								human.rectangle = human.canvas.create_rectangle(human.x,human.y,human.x,human.y,fill=human.color,outline='')
								human.chased_by = self
						else:
							if human.chased_by == self:
								human.chased_by = 0
			self.canvas.move(self.rectangle, move_x, move_y)
			self.canvas.move(self.circle, move_x, move_y)
	def fire(self):
		if self.weapons[self.weapon] == 'fire':
			if len(self.shot)<=10:
				if randint(0,1) == 1:
					sound = mixer.Sound("flamethrower1.ogg")
				else:
					sound = mixer.Sound("flamethrower2.ogg")
				sound.set_volume(.5)
				sound.play()
				self.shot.append(fire_shot(self.canvas, self, self.zombies, self.buildings, self.shot))
		elif self.weapons[self.weapon] == 'shotgun':
			if len(self.shot)<=6:
				sound = mixer.Sound("shotgun.ogg")
				sound.set_volume(.5)
				sound.play()
				self.shot.append(shotgun_shot(self.canvas, self, self.zombies, self.buildings, self.shot))
		elif self.weapons[self.weapon] == 'chainsaw':
			already_in_use = False
			for shot in self.shot:
				if shot.type == 'chainsaw':
					already_in_use = True
					break
			if not already_in_use:
				self.shot.append(chainsaw(self.canvas, self, self.zombies, self.buildings, self.shot))
		elif self.weapons[self.weapon] == 'pistol':
			if len(self.shot)<=12 and time()-self.last_shot>.5:
				sound = mixer.Sound("pistol.ogg")
				sound.set_volume(.5)
				sound.play()
				self.shot.append(pistol_shot(self.canvas, self, self.zombies, self.buildings, self.shot))
				self.last_shot = time()